/* ------------------------------------------------------------------------*
 *
 * ------------------------------------------------------------------------*/
#define _RTC_TIME_MODULE_
#include "rtctime.h"

/*****************************************************************************//*!
* @brief   Rtc run .
*          once per sec , in timer int     
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void TimeRun(void)
{
    TimeCur.Sec++;
    
    if(TimeCur.Sec > 59)
    {
        TimeCur.Sec = 0;
        TimeCur.Min ++;
    }
    
	if(TimeCur.Min > 59)
	{
		TimeCur.Min -= 60;
		TimeCur.Hour ++;
		if(TimeCur.Hour > 23)
		{
		    TimeCur.Hour = 0;
		    TimeCur.Day ++;
			TimeCur.Week++;
			
			if(TimeCur.Week > 7)
				TimeCur.Week = 1;
			
			if( ((TimeCur.Mon & 0x01) ^ (TimeCur.Mon >> 3)) & 0x0F) 
			{
                //1 3 5 7 8 10 12
			 	if(TimeCur.Day > 31)
				{
					TimeCur.Day = 1;
					TimeCur.Mon ++;
				}
			}
			else if(TimeCur.Mon == 2)
			{
                //2
				if( ( ( ( TimeCur.Year & 0x03 ) == 0 ) && ( ( TimeCur.Year % 100 ) != 0)) || ( ( TimeCur.Year % 400 ) == 0 ) )
				{
                    //run nian
			 		if(TimeCur.Day > 29)
					{
						TimeCur.Day = 1;
						TimeCur.Mon ++;
					}				
				}
				else
				{
                    //ping nian
			 		if(TimeCur.Day > 28)
					{
						TimeCur.Day = 1;
						TimeCur.Mon ++;
					}				
				}			
			}
			else
			{
                //4 6 9 11
			 	if(TimeCur.Day > 30)
				{
					TimeCur.Day = 1;
					TimeCur.Mon ++;
				}			
			}

		    if(TimeCur.Mon > 12)
			{
				TimeCur.Mon = 1;
				TimeCur.Year ++;
			}
		}
	}
}

/*****************************************************************************//*!
* @brief   Time set .
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void TimeSet(TIME_Type *pTime)
{
    TimeCur.Year = pTime->Year;
    TimeCur.Mon = pTime->Mon;
    TimeCur.Day = pTime->Day;
	TimeCur.Week = pTime->Week;
    TimeCur.Hour = pTime->Hour;
    TimeCur.Min = pTime->Min;
    TimeCur.Sec = pTime->Sec;
    
    CenturyCur =  TimeCur.Year / 100;
    CenturyCur *= 100; 
}

/*****************************************************************************//*!
* @brief   Time get .
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void TimeGet(TIME_Type *pTime)
{
    pTime->Year = TimeCur.Year ;
    pTime->Mon = TimeCur.Mon ;
    pTime->Day = TimeCur.Day ;
	pTime->Week = TimeCur.Week ;
    pTime->Hour = TimeCur.Hour ;
    pTime->Min = TimeCur.Min ;
    pTime->Sec = TimeCur.Sec ;   
}

/*****************************************************************************//*!
* @brief   Time copy .
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void TimeCopy(TIME_Type *pTimeSrc,TIME_Type *pTimeDes)
{
    pTimeDes->Year = pTimeSrc->Year ;
    pTimeDes->Mon  = pTimeSrc->Mon ;
    pTimeDes->Day  = pTimeSrc->Day ;
	pTimeDes->Week  = pTimeSrc->Week ;
    pTimeDes->Hour = pTimeSrc->Hour ;
    pTimeDes->Min  = pTimeSrc->Min ;
    pTimeDes->Sec  = pTimeSrc->Sec ;   
}

/*****************************************************************************//*!
* @brief   Time compare .
*               
* @param   none
*
* @return  == : 0        >: 1           <: -1 
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
int8_t TimeCompare(TIME_Type *pTime1,TIME_Type *pTime2)
{
    if(pTime1->Year == pTime2->Year)
    {
        if(pTime1->Mon == pTime2->Mon)
        {
            if(pTime1->Day == pTime2->Day)
            {
                if(pTime1->Hour == pTime2->Hour)
                {
                    if(pTime1->Min == pTime2->Min)
                    {
                        if(pTime1->Sec == pTime2->Sec)
                        {
                            return 0;
                        }
                        else if(pTime1->Sec > pTime2->Sec){return ((int8_t)(1));}
                        else{return ((int8_t)(-1));}// End compare - Sec
                    }
                    else if(pTime1->Min > pTime2->Min){return ((int8_t)(1));}
                    else{return ((int8_t)(-1));}// End compare - Min
                }
                else if(pTime1->Hour > pTime2->Hour){return ((int8_t)(1));}
                else{return ((int8_t)(-1));}// End compare - Hour
            }
            else if(pTime1->Day > pTime2->Day){return ((int8_t)(1));}
            else{return ((int8_t)(-1));}// End compare - Day
        }
        else if(pTime1->Mon > pTime2->Mon){return ((int8_t)(1));}
        else{return ((int8_t)(-1));}// End compare - Mon
    }
    else if(pTime1->Year > pTime2->Year){return ((int8_t)(1));}
    else{return ((int8_t)(-1));}// End compare - Year
}

/*****************************************************************************//*!
* @brief   rtc time pulse creat .
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void RtcTimePulseCreat(void){
    bRtcPulse_1s = 0;
    if(RtcTimeSecLast != TimeCur.Sec){
        RtcTimeSecLast++;
        if(RtcTimeSecLast>59)
            RtcTimeSecLast=0;
        bRtcPulse_1s=1;
    }
}
